<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="商家名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商家名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="tel1">
        <el-input
          v-model="queryParams.tel1"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="规模/人" prop="scale">
        <el-input
          v-model="queryParams.scale"
          placeholder="请输入商家规模"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择商家类型" clearable size="small">
          <el-option label="娱乐" value="1" />
          <el-option label="教育" value="2" />
          <el-option label="文学" value="3" />
          <el-option label="饮食" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="begindate">
        <el-date-picker clearable size="small"
          v-model="queryParams.begindate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择活动开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="enddate">
        <el-date-picker clearable size="small"
          v-model="queryParams.enddate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择活动结束时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="请输入商家联系人名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="businessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="赞助商主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="商家名称" align="center" prop="name" />
      <el-table-column label="商家地址" align="center" prop="place" />
      <el-table-column label="联系人" align="center" prop="contacts" />

      <el-table-column label="联系方式" align="center" prop="tel1" />
      <el-table-column label="规模/人" align="center" prop="scale" />
      <el-table-column label="商家类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="宣传详情" align="center" prop="businesscontext"  show-overflow-tooltip/>
      <el-table-column label="活动开始时间" align="center" prop="begindate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.begindate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="enddate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改赞助商主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家图片" prop="picture">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/master/myActivity/avatar"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.picture" :src="form.picture" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商家地址" prop="place">
          <el-input v-model="form.place" placeholder="请输入商家地址" />
        </el-form-item>
        <el-form-item label="商家名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商家名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入商家联系人姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel1">
          <el-input v-model="form.tel1" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="规模/人" prop="scale">
          <el-input v-model="form.scale" placeholder="请输入商家规模" />
        </el-form-item>
        <el-form-item label="商家类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择商家类型">
            <el-option label="娱乐" value="1" />
            <el-option label="教育" value="2" />
            <el-option label="文学" value="3" />
            <el-option label="饮食" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="宣传详情" prop="businesscontext">
          <el-input v-model="form.businesscontext" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动开始时间" prop="begindate">
          <el-date-picker clearable size="small"
            v-model="form.begindate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="enddate">
          <el-date-picker clearable size="small"
            v-model="form.enddate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择活动结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
import { listBusiness, getBusiness, delBusiness, addBusiness, updateBusiness, exportBusiness } from "@/api/master/business";

export default {
  name: "Business",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 赞助商主表格数据
      businessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        place: undefined,
        name: undefined,
        tel1: undefined,
        tel2: undefined,
        picture: undefined,
        scale: undefined,
        type: undefined,
        businesscontext: undefined,
        begindate: undefined,
        enddate: undefined,
        createrdate: undefined,
        updatetime: undefined,
        contacts: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scale: [{
          trigger: "blur",
          pattern: /^[0-9]*$/,
          message: "请输入正确的人数格式",
        }],
        tel1: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        place: [{ required: true, message: "请输入商家地址", trigger: "blur" }],
        name: [{ required: true, message: "请输入商家名称", trigger: "blur" }],
        contacts: [{ required: true, message: "请输入商家联系人", trigger: "blur" }],
        type: [{ required: true, message: "请输入商家类型", trigger: "blur" }],
        picture: [{ required: true, message: "请输入商家图片", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询赞助商主列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then(response => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        place: undefined,
        name: undefined,
        tel1: undefined,
        tel2: undefined,
        picture: undefined,
        scale: undefined,
        type: undefined,
        businesscontext: undefined,
        begindate: undefined,
        enddate: undefined,
        createrdate: undefined,
        updatetime: undefined,
        contacts: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加赞助商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBusiness(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改赞助商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBusiness(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusiness(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const name = row.name || this.names[0];
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除赞助商："' + name + '"?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBusiness(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有赞助商主数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBusiness(queryParams);
        }).then(response => {
          this.download(response.msg);
      })
    },

    typeFormat(row, colum) {
      if (row.type == 1) {
        return '娱乐'
      } else if (row.type == 2) {
        return '教育'
      } else if (row.type == 3) {
        return '文学'
      } else if (row.type == 4) {
        return '饮食'
      } else {
        return '未知类型'
      }
    },

    handleAvatarSuccess(res, file) {
      this.form.picture = process.env.VUE_APP_BASE_API + res.imgUrl;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 20;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 20 MB!');
      }
      return isLt2M;
    },
  }
};
</script>
